package com.foreveross.taskservice.application.impl.task.statsdetail;

import java.util.List;

import javax.annotation.Resource;

import org.dayatang.querychannel.Page;
import org.springframework.stereotype.Service;

import com.foreveross.taskservice.application.impl.convert.AirFreightChannelConvert;
import com.foreveross.taskservice.application.task.statsdetail.ITaskDetailApplication;
import com.foreveross.taskservice.application.task.statsdetail.vo.BaseChannelVO;
import com.foreveross.taskservice.application.task.statsdetail.vo.TaskServicePage;
import com.foreveross.taskservice.common.CacheDataSpace;
import com.foreveross.taskservice.common.TaskStatusEnum;
import com.foreveross.taskservice.common.model.AbstractTaskModel;
import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.foreveross.taskservice.infra.ddd.TaskserviceEntityHibernateRepository;
import com.foreveross.taskservice.infra.util.DateUtil;
import com.foreveross.taskservice.taskcache.ITaskCacheQuery;
@Service
public class TaskDetailApplicationImpl extends TaskserviceEntityHibernateRepository implements ITaskDetailApplication{
	@Resource(name="redisTaskCacheQueryImpl")
	private ITaskCacheQuery taskCacheQuery;
	
	@Override
	public TaskServicePage<AbstractTaskModel> query(long channelId, int status, String grabDateStr,int pageLen,int pageIndex) throws Exception {
		String todayStr=DateUtil.getNowDateStr("yyyy-MM-dd");
		
		TaskServicePage<AbstractTaskModel> page = null;
		if(grabDateStr.equals(todayStr)){//当前抓取日期，从redis读取数据
			pageLen=getPageLen(pageLen);
			pageIndex=getPageIndex(pageIndex);
			GradChannelEntity channel=get(GradChannelEntity.class, channelId);
			List<AbstractTaskModel> tasks=null;
			long totalSize=0;
			
			if(channel!=null){
				tasks=taskCacheQuery.queryTaskDetailInfo(CacheDataSpace.AirFreightSpace(), channelId,TaskStatusEnum.getTaskStatusEnum(status),pageLen*pageIndex, pageLen);
				totalSize=taskCacheQuery.queryTaskSize(CacheDataSpace.AirFreightSpace(), channelId,TaskStatusEnum.getTaskStatusEnum(status));
				page=new TaskServicePage<AbstractTaskModel>(pageIndex, totalSize, pageLen,tasks);
				BaseChannelVO channelVO=new BaseChannelVO();
				channelVO.setChannelModel(AirFreightChannelConvert.convert(channel));
				channelVO.setChannelId(channel.getId());
				channelVO.setName(channel.getName());
				page.setChannel(channelVO);
				page.setTaskStatus(status);
			}
			
		}else{//历史数据从hbase读取
//			List<Filter> filterList = new ArrayList<Filter>();
//			List<AbstractTaskModel>list=null;
//			SingleColumnValueFilter singleFilter = new SingleColumnValueFilter("planeinfo".getBytes(), "carrierKey".getBytes(), CompareOp.EQUAL, "".getBytes());
//			SingleColumnValueFilter singleFilter2 = new SingleColumnValueFilter("planeinfo".getBytes(), "flightBatchDate".getBytes(), CompareOp.EQUAL, "".getBytes());
//			filterList.add(singleFilter);
//		 	filterList.add(singleFilter2);
//			String startRowKey="",stopRowKey="";
//			// getRecords(String tableName,tring startRowKey, String stopRowKey, String[] family,List<Filter> filters)
//			ResultScanner sc=HBaseHelper.getRecords("CRAWL_DETAIL_INFO", startRowKey,stopRowKey,new String[]{"grawlInfo"},filterList);
//			HBaseHelper.printResult(sc);
//			if(sc !=null ){
//				list=HBaseDetailInfoHelper.transformHBaseResultVOs(sc);
//			}
//			if(list.size()>0){
//				
//			}
			
		}
		
		return page;
	}
	
	
	
	
	private int getPageLen(int pageLen){
		if(pageLen<=0 || pageLen>TaskServicePage.DEFAULT_PAGE_MAXSIZE){
			return Page.DEFAULT_PAGE_SIZE;
		}
		return pageLen;
	}
	
	private int getPageIndex(int pageIndex){
		if(pageIndex<0){
			return 0;
		}
		return pageIndex;
	}

	

	
}
